package com.woniuxy.oldmanapi.controller;

import com.woniuxy.oldmanapi.entity.Recipecategory;
import com.woniuxy.oldmanapi.service.RecipeCategoryService;
import com.woniuxy.oldmanapi.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RecipeGoryController {
    @Autowired
    RecipeCategoryService recipecategoryService;

    @GetMapping("/recipeCategory/list")
    public ResponseResult<List<Recipecategory>> initSelectAll() {
        ResponseResult<List<Recipecategory>> result = new ResponseResult<>();
        try {
            List<Recipecategory> pageInfo = recipecategoryService.initSelectAll();
            if (pageInfo == null) {
                result.setCode(204);
                result.setMsg("无食谱类别,请先添加");
            } else {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            }
        } catch (Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
    @PostMapping("/recipeCategory/addRecipeGory")
    public ResponseResult<Void> addRecipeCategory(@RequestBody Recipecategory recipe) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            recipecategoryService.addRecipe(recipe);
            result.setCode(200);
            result.setMsg("添加成功");
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @PutMapping("/recipeCategory/modifyRecipe")
    public ResponseResult<Void> modifyRecipe(@RequestBody Recipecategory recipe) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            recipecategoryService.modifyRecipe(recipe);
            result.setCode(200);
            result.setMsg("修改成功");
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @DeleteMapping("/recipeCategory/deleteRecipeCategory/{id}")
    public ResponseResult<Void> deleteRecipe(@PathVariable("id") Integer id) {
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            recipecategoryService.deleteRecipe(id);
            result.setCode(200);
            result.setMsg("删除成功");

        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }

    @GetMapping("/recipeCategory/selectRecipeGoryById/{id}")
    public ResponseResult<Recipecategory> selectRecipeById(@PathVariable("id") Integer id) {
        ResponseResult<Recipecategory> result = new ResponseResult<>();
        try {
            Recipecategory recipe = recipecategoryService.selectRecipeById(id);
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(recipe);
        } catch (
                Exception e) {
            result.setCode(201);
            result.setMsg("系统繁忙,请稍后再试");
        }
        return result;
    }
}
